package com.shizhanzhe.zxbapp.home;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.shizhanzhe.zxbapp.R;
import com.shizhanzhe.zxbapp.bean.AppDetail;
import com.shizhanzhe.zxbapp.bean.UserBean;

import java.util.List;

/**
 * Created by zz9527 on 2018/11/14.
 */
public class AppDetailAdapter extends BaseAdapter {
    private final LayoutInflater inflater;
    private List<AppDetail.DetailBean> mList;
    private final Context context;
    private final double price;
    public AppDetailAdapter(Context context, List<AppDetail.DetailBean> list,double price) {
        this.context = context;
        this.mList = list;

        this.price=price;
        inflater = LayoutInflater.from(context);
    }

    public void setData(List<AppDetail.DetailBean> list) {
        mList = list;
    }

    @Override
    public int getCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public AppDetail.DetailBean getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = inflater.inflate(R.layout.lv_item_task_desc, null);
            viewHolder.tv_id = (TextView) convertView.findViewById(R.id.lvitem_tv_ext_id);
            viewHolder.tv_desc = (TextView) convertView.findViewById(R.id.lvitem_tv_ext_desc);
            viewHolder.tv_status = (TextView) convertView.findViewById(R.id.lvitem_tv_ext_status);
            viewHolder.tv_points = (TextView) convertView.findViewById(R.id.lvitem_tv_ext_points);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        AppDetail.DetailBean bean = mList.get(position);

        // 设置左边的数字序号
        viewHolder.tv_id.setText(String.valueOf(position + 1));
        // 设置任务赚取描述
        viewHolder.tv_desc.setText(bean.getDname());
        // 设置积分或者已完成
        viewHolder.tv_points.setText("+" + bean.getDprice() + "元");
        Drawable rightDrawable = context.getResources().getDrawable(R.mipmap.share);
        rightDrawable.setBounds(0, 0, rightDrawable.getMinimumWidth(), rightDrawable.getMinimumHeight());
        switch (bean.getInstatus()) {
            // 任务可以做
            case 0:
                viewHolder.tv_points.setCompoundDrawables(rightDrawable, null, null, null);
                viewHolder.tv_points.setBackgroundResource(R.drawable.bg_money);
                viewHolder.tv_points.setTextColor(context.getResources().getColor(R.color.ff9d0f));
                viewHolder.tv_status.setText("任务可进行");
                break;
            // 任务已完成
            case 1:
                viewHolder.tv_points.setText("+" + bean.getDprice() + "元");
                viewHolder.tv_points.setTextColor(context.getResources().getColor(R.color.red));
                viewHolder.tv_status.setText("任务已完成");
                viewHolder.tv_points.setCompoundDrawables(null, null, null, null);
                viewHolder.tv_points.setBackgroundResource(0);
                break;
            case 2:
                viewHolder.tv_points.setTextColor(context.getResources().getColor(R.color.color_huise));
                viewHolder.tv_status.setText("任务已过期");
                viewHolder.tv_points.setText("过期");
                viewHolder.tv_points.setCompoundDrawables(null, null, null, null);
                viewHolder.tv_points.setBackgroundResource(0);
                break;
            default:
                break;
        }

        return convertView;
    }

    private static class ViewHolder {
        TextView tv_id;
        TextView tv_desc;
        TextView tv_status;
        TextView tv_points;
    }
}
